from pydantic import BaseModel, Field
from datetime import datetime
from typing import List, Optional
from enum import Enum

class TaskCategory(str, Enum):
    meeting = "meeting"
    notification = "notification"
    document = "document"
    family = "family"
    project = "project"
    personal = "personal"

class TaskStatus(str, Enum):
    pending = "pending"
    in_progress = "in-progress"
    completed = "completed"
    overdue = "overdue"

class Priority(str, Enum):
    low = "low"
    medium = "medium"
    high = "high"
    urgent = "urgent"

class Attachment(BaseModel):
    id: str
    name: str
    type: str
    size: int
    url: str
    uploaded_at: datetime

class Task(BaseModel):
    id: Optional[str] = Field(None, alias="_id")
    title: str
    category: TaskCategory
    description: Optional[str] = None
    start_time: datetime = Field(alias="startTime")
    deadline: datetime
    status: TaskStatus
    attachments: List[Attachment] = []
    folder_path: str = Field(alias="folderPath")
    priority: Priority
    created_at: datetime = Field(default_factory=datetime.utcnow, alias="createdAt")
    updated_at: datetime = Field(default_factory=datetime.utcnow, alias="updatedAt")

    class Config:
        populate_by_name = True
        json_encoders = {
            datetime: lambda v: v.isoformat()
        }

class TaskCreate(BaseModel):
    title: str
    category: TaskCategory
    description: Optional[str] = None
    start_time: datetime = Field(alias="startTime")
    deadline: datetime
    status: TaskStatus = TaskStatus.pending
    attachments: List[Attachment] = []
    folder_path: str = Field(alias="folderPath")
    priority: Priority = Priority.medium

    class Config:
        populate_by_name = True

class TaskUpdate(BaseModel):
    title: Optional[str] = None
    category: Optional[TaskCategory] = None
    description: Optional[str] = None
    start_time: Optional[datetime] = Field(None, alias="startTime")
    deadline: Optional[datetime] = None
    status: Optional[TaskStatus] = None
    attachments: Optional[List[Attachment]] = None
    folder_path: Optional[str] = Field(None, alias="folderPath")
    priority: Optional[Priority] = None
    updated_at: datetime = Field(default_factory=datetime.utcnow, alias="updatedAt")

    class Config:
        populate_by_name = True

class FileMetadata(BaseModel):
    id: Optional[str] = Field(None, alias="_id")
    original_name: str
    safe_filename: str
    content_type: str
    size: int
    file_path: str
    file_url: str
    folder_path: Optional[str] = None
    uploaded_at: datetime = Field(default_factory=datetime.utcnow)
    task_id: Optional[str] = None

    class Config:
        populate_by_name = True
